//
//  ZZTabBar.swift
//  web
//
//  Created by 张卓 on 2017/1/16.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZTabBar: UITabBar {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //1. 声明闭包
    var closuer:(()->())?
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(bnt)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算UITabBarButton的宽   5个按钮
         let w = ZZSCREENW * 0.2
        //确定button的位置
        var index:CGFloat = 0
        
        for view in subviews {
            //判断是否属于UITabBarButton
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                view.frame.size.width = w
                view.frame.origin.x = index * w
                index += 1
                
                if index == 2 {
                    index += 1
                }
            }
        }
        //设置button的frame
        bnt.center.x = frame.size.width * 0.5
        bnt.center.y = frame.size.height * 0.5
    }
    //点击事件
    @objc private func bntClick(){
        //执行闭包
        closuer?()
        
    }
    
    
    
    //懒加载一个button   私有属性
    private lazy var bnt:UIButton = {
        
        let bnt = UIButton()
        
        //设置图片
        bnt.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        bnt.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        bnt.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        bnt.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        //设置button自适应大小
        bnt.sizeToFit()
        
        //添加点击事件
        bnt.addTarget(self, action: #selector(bntClick), for: UIControlEvents.touchUpInside)
        
        
        return bnt
    }()
    

}
